package app_kvEcs;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.Level;

import logger.LogSetup;
import app_kvClient.Constants;
import app_kvServer.ServerConfig;

/**
 * @author Cipri_L
 *	
 * step1 - initService with n nodes. SSH command to run the jar file
 * step2 - start server_i and gets the memory location. the server is now up and running.
 */
public class ECSClient {

	private ECS controller ;
	
	public ECSClient(){
		controller = new ECS();
	}
	
	private static void display(String message){
		System.out.println(message);
	}
	
	/**
	 * Launch KVServer process at the given number of nodes by SSH calls and initialize them with meta-data.
	 * @param nodes
	 */
	public void initService(int nodes){
		String message = "ECSClient>>initService with "+ nodes +" nodes";
		try {
			controller.initService(nodes, true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		display(message);
		
	}
	
	/**
	 * start storage service for serving client requests
	 */
	public void start(){
		String message = "start server";
		
		
		controller.start();
		display(message);
	}

	/**
	 * stop storage service; remote processes remain alive
	 */
	public void stop(){
		String message = "stop server";
		controller.stop();
		display(message);
	}
	
	/**
	 * exit remote processes 
	 */
	public void shutDown(){
		String message = "shutdown server";
		display(message);
		controller.shutDown();
	}
	
	/**
	 * add a new storage node at arbitrary position
	 */
	public void addNode(){
		String message = "add node";
		display(message);
		controller.addNode();
	}
	
	/**
	 * remove storage node at arbitrary position
	 */
	public void removeNode(){
		String message = "remove node";
		display(message);
		controller.removeNode();
	}
	

	private void readCommands() throws IOException {

		BufferedReader cons = new BufferedReader(new InputStreamReader(	System.in));
		boolean quit = false;
		while (!quit) {
			System.out.print("ECSClient>");
			String input = "";
			try {
				input = cons.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			String[] tokens = input.trim().split("\\s+");
			if (tokens[0].trim().equals("init")) {
				if(tokens.length < 2){
					System.out.println("Error: number of servers missing!");
					continue;
				}
				try{
					String s = tokens[1];
					int nodes = Integer.parseInt(s);
					this.initService(nodes);
				} catch (Exception e){
					System.out.println("Error: not a valid number!");
				}
			}else if (input.trim().equals("start")) {				
				this.start();
			}else if (input.trim().equals("stop")) {
				this.stop();
			}else if (input.trim().equals("shutdown")) {
				this.shutDown();
			}else if (input.trim().equals("addnode")) {
				this.addNode();
			}else if (input.trim().equals("removenode")) {
				this.removeNode();
			}else if (input.trim().equals("exit")) {
				this.shutDown();
				quit = true;
			}else if (input.trim().equals("help")) {
				String commandsList = "commands: " + "init [nodes] | start | stop | shutdown | addnode | removenode | help | exit";
				System.out.println(commandsList);
			}else {
				System.out.println("UnknownCommand! - type: help");
			}
		}
	}
	
	
	
	public static void main ( String[] args )
    {
		String file = "";
		if(args.length < 1){
			String message = "No Configuration file! Loading default config ...";
			System.out.println(message);
			file = "server.config";
		} else {
			file = args[0];
		}
		
		ECSClient ecs = new ECSClient();
		
		
    	try {
			ecs.readCommands();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
	
	
}
